package team.bluepen.supermarket.data;

import team.bluepen.supermarket.constant.Error;
import team.bluepen.supermarket.constant.Role;
import team.bluepen.supermarket.data.dao.GoodDatabase;
import team.bluepen.supermarket.data.dao.UserDatabase;
import team.bluepen.supermarket.data.entity.User;
import team.bluepen.supermarket.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取单例数据库
 * @author Kai
 */
public class DatabaseHelper {
    private static final String TAG = "DatabaseHelper";
    private static volatile UserDatabase USER_DATABASE;
    private static volatile GoodDatabase GOOD_DATABASE;

    private DatabaseHelper() {}

    /**
     * 获取单例{@link UserDatabase}
     * @return {@link UserDatabase}
     */
    public static UserDatabase getUserDatabase() {
        if (USER_DATABASE == null) {
            synchronized (DatabaseHelper.class) {
                if (USER_DATABASE == null) {
                    try {
                        USER_DATABASE = new UserDatabase();
                    } catch (IOException e) {
                        Log.e(TAG, "Open database failed! ", e);
                    }
                }
            }
        }
        return USER_DATABASE;
    }

    /**
     * 获取单例{@link GoodDatabase}
     * @return {@link GoodDatabase}
     */
    public static GoodDatabase getGoodDatabase() {
        if (GOOD_DATABASE == null) {
            synchronized (DatabaseHelper.class) {
                if (GOOD_DATABASE == null) {
                    try {
                        GOOD_DATABASE = new GoodDatabase();
                    } catch (IOException e) {
                        Log.e(TAG, "Open database failed! ", e);
                    }
                }
            }
        }
        return GOOD_DATABASE;
    }

    public static void closeUserDatabase() {
        if (USER_DATABASE != null && USER_DATABASE.isOpen()) {
            try {
                USER_DATABASE.close();
            } catch (IOException e) {
                Log.e(TAG, "Close database failed! ", e);
            }
        }
    }

    public static void closeGoodDatabase() {
        if (GOOD_DATABASE != null && GOOD_DATABASE.isOpen()) {
            try {
                GOOD_DATABASE.close();
            } catch (IOException e) {
                Log.e(TAG, "Close database failed! ", e);
            }
        }
    }

    /**
     * 启动初始化
     */
    public static void initDatabase() {
        Error userState = getUserDatabase().create();
        Error goodState = getGoodDatabase().create();

        Log.i(TAG, "User Database create State: " + userState +
                ", Good Database create State: " + goodState);

        User admin = new User()
                .setId(1)
                .setUsername("admin")
                .setPassword("123456")
                .setRole(Role.ADMIN)
                .generateRowKey();
        User user = new User()
                .setId(2)
                .setUsername("user1")
                .setPassword("123456")
                .setRole(Role.USER)
                .generateRowKey();
        User guest = new User()
                .setId(3)
                .setUsername("guest1")
                .setPassword("123456")
                .setRole(Role.GUEST)
                .generateRowKey();
        User user2 = new User()
                .setId(4)
                .setUsername("user2")
                .setPassword("123456")
                .setRole(Role.USER)
                .generateRowKey();
        List<User> users = new ArrayList<>();
        users.add(admin);
        users.add(user);
        users.add(guest);
        users.add(user2);
        getUserDatabase().insert(users);
    }
}
